package cvi.com.reader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.PartBranchBean;
import cvi.com.reader.bean.constant;
import cvi.com.reader.network.DefaultObserver;
import cvi.com.reader.network.NetWorkManager;
import cvi.com.reader.tool.UtilsOnclickListen;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 梅涛 on 2020/11/19.
 */

public class UpdataPartBranchActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tittle)
    TextView tittle;
    @BindView(R.id.right_icon)
    LinearLayout rightIcon;
    @BindView(R.id.party_branch)
    EditText partyBranch;
    @BindView(R.id.party_branch_spinner)
    Spinner partyBranchSpinner;
    @BindView(R.id.sava_tv)
    TextView savaTv;
    @BindView(R.id.s_v)
    Switch sV;
    @BindView(R.id.party_branch_spinner1)
    Spinner partyBranchSpinner1;
    private String party = "";
    private String isParty = "1";
    private List<String> dataPartBeanListName = new ArrayList<>();
    private List<String> dataCompanyBeanListName = new ArrayList<>();
    private ArrayAdapter<String> adapter = null;
    private ArrayAdapter<String> adapter1 = null;
    private String is_party_member = "0";
    private List<PartBranchBean.DataBean> partDataBeanList = new ArrayList<>();

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sava_tv:
                if (UtilsOnclickListen.isFastClick()) {
                    SaveData();
                }
                break;
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_updata_party_branch;
    }

    @Override
    protected void initView() {
        savaTv.setOnClickListener(this);
        back.setOnClickListener(this);
        tittle.setText("修改党支部/公司");
        party = getIntent().getStringExtra("part");
        is_party_member = getIntent().getStringExtra("is_party_member");
        partyBranch.setText(party);
        sV.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    isParty = "1";
                    setData("1");

                } else {
                    isParty = "0";
                    setData("0");
                }
                Log.d("vfaqvfv", isParty);
                //控制开关字体颜色

            }
        });
    }

    @Override
    protected void initData() {
        if ("1".equals(is_party_member)) {
            partyBranchSpinner.setVisibility(View.VISIBLE);
            partyBranchSpinner1.setVisibility(View.GONE);
            sV.setChecked(true);
        } else {
            partyBranchSpinner1.setVisibility(View.VISIBLE);
            partyBranchSpinner.setVisibility(View.GONE);
            sV.setChecked(false);
        }
        //党支部接口
        NetWorkManager.getInstance().getNetWorkApiService().getPartBranch().compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(UpdataPartBranchActivity.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            setPartyBranch(response);
                        }

                    }
                });
    }

    //党支部列表数据
    private void setPartyBranch(JSONObject response) {
        PartBranchBean partBranchBean = response.toJavaObject(PartBranchBean.class);


        partDataBeanList = partBranchBean.getData();
        for (int i = 0; i < partDataBeanList.size(); i++) {

            if ("1".equals(String.valueOf(partDataBeanList.get(i).getType()))) {
                dataPartBeanListName.add(partDataBeanList.get(i).getName());
            } else if ("0".equals(String.valueOf(partDataBeanList.get(i).getType()))) {

                dataCompanyBeanListName.add(partDataBeanList.get(i).getName());
            }

        }

        if ("1".equals(is_party_member)) {
            for (int i = 0; i < dataPartBeanListName.size(); i++) {
                if (party.isEmpty()) {
                    return;
                } else {
                    if (party.equals(dataPartBeanListName.get(i))) {
                        int pos = dataPartBeanListName.indexOf(dataPartBeanListName.get(i));
                        Collections.swap(dataPartBeanListName, pos, 0);
                    }
                }

            }

            if (0 == dataPartBeanListName.size()) {
                partyBranchSpinner.setVisibility(View.GONE);
            } else {
                partyBranchSpinner.setVisibility(View.VISIBLE);
            }
        } else {
            for (int i = 0; i < dataCompanyBeanListName.size(); i++) {
                if (party.isEmpty()) {
                    return;
                } else {
                    if (party.equals(dataCompanyBeanListName.get(i))) {
                        int pos = dataCompanyBeanListName.indexOf(dataCompanyBeanListName.get(i));
                        Collections.swap(dataCompanyBeanListName, pos, 0);
                    }
                }

            }
            if (0 == dataCompanyBeanListName.size()) {
                partyBranchSpinner1.setVisibility(View.GONE);
            } else {
                partyBranchSpinner1.setVisibility(View.VISIBLE);
            }
        }


        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataPartBeanListName);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定 Adapter到控件
        partyBranchSpinner.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        partyBranchSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                partyBranch.setText(parent.getAdapter().getItem(pos).toString());

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });


        adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataCompanyBeanListName);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定 Adapter到控件
        partyBranchSpinner1.setAdapter(adapter1);
        adapter1.notifyDataSetChanged();

        partyBranchSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {

                partyBranch.setText(parent.getAdapter().getItem(pos).toString());

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private void SaveData() {
        if (partyBranch.getText().toString().trim().isEmpty()) {
            Toast.makeText(UpdataPartBranchActivity.this, "内容不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("party_branch", partyBranch.getText().toString().trim());
        map.put("is_party_member", isParty);
        //党支部接口
        NetWorkManager.getInstance().getNetWorkApiService().updataPersonInfo(map).compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(UpdataPartBranchActivity.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            Toast.makeText(UpdataPartBranchActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            intent.setAction(constant.UPDATA_PARTY_MENBER_INFO_MYRECEIVER);
                            sendBroadcast(intent);
                            finish();
                        }

                    }
                });

    }

    private void setData(String flag) {

        if (adapter == null) {
            return;
        }

        if ("1".equals(flag)) {
            partyBranchSpinner1.setVisibility(View.GONE);
            partyBranchSpinner.setVisibility(View.VISIBLE);
            if (dataPartBeanListName.size() > 0) {
                partyBranchSpinner.setVisibility(View.VISIBLE);
                partyBranch.setText(dataPartBeanListName.get(0));
            } else {
                partyBranchSpinner.setVisibility(View.GONE);
                partyBranch.setText("");
                partyBranch.setHint("请输入支部名称/公司名字");
            }

        } else {
            partyBranchSpinner.setVisibility(View.GONE);
            partyBranchSpinner1.setVisibility(View.VISIBLE);
            if (dataCompanyBeanListName.size() > 0) {
                partyBranchSpinner1.setVisibility(View.VISIBLE);
                partyBranch.setText(dataCompanyBeanListName.get(0));
                Log.d("asddd444", partyBranch.getText().toString() + "===" + dataCompanyBeanListName.get(0) + "flag=" + flag);
            } else {
                partyBranchSpinner1.setVisibility(View.GONE);
                partyBranch.setText("");
                partyBranch.setHint("请输入支部名称/公司名字");
            }
        }
        adapter.notifyDataSetChanged();
        adapter1.notifyDataSetChanged();
    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
